﻿Imports SHDocVw
Imports mshtml
Imports System.Threading
Imports System.Windows.Forms
Imports System.Net.Mail
Imports log4net
Imports log4net.Config
Imports System.Reflection.Assembly
Imports System.Reflection

Public Class modMain

    Public Const REG_APP_NAME As String = "BOCDN"
    Public Const REG_APP_MAIN As String = "Main"

    Private Shared ReadOnly systemLog As ILog = LogManager.GetLogger("logger")

    Shared Sub New()
        XmlConfigurator.Configure()
    End Sub

    <System.STAThread()>
    Shared Sub main()
        Try

            systemLog.Warn("************ Starting, version:" & Assembly.GetEntryAssembly.GetName.Version.ToString)
            Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs

            If CommandLineArgs.Count = 1 AndAlso CommandLineArgs(0) = "dn" Then
                systemLog.Info("Will call boc download")
                Dim boc As New BocDownloader

                boc.BocPassword = Encrypt.Decrypt(GetSetting(REG_APP_NAME, REG_APP_MAIN, "BOCPass", String.Empty))
                boc.BocUsername = Encrypt.Decrypt(GetSetting(REG_APP_NAME, REG_APP_MAIN, "BOCUname", String.Empty))
                boc.SmtpPass = Encrypt.Decrypt(GetSetting(REG_APP_NAME, REG_APP_MAIN, "GPass", String.Empty))
                boc.SmtpUname = Encrypt.Decrypt(GetSetting(REG_APP_NAME, REG_APP_MAIN, "GUname", String.Empty))
                boc.Email1 = GetSetting(REG_APP_NAME, REG_APP_MAIN, "Email1", String.Empty)
                boc.Email2 = GetSetting(REG_APP_NAME, REG_APP_MAIN, "Email2", String.Empty)
                boc.SmtpServer = GetSetting(REG_APP_NAME, REG_APP_MAIN, "SmtpServer", String.Empty)
                boc.SmtpPort = CInt(GetSetting(REG_APP_NAME, REG_APP_MAIN, "SmtpPort", "587"))
                boc.ieVisible = True
                If boc.configComplete() Then
                    boc.runDownload()
                Else
                    systemLog.Info("Config was not complete, will bring up config form.")
                    Call ShowAndAllowSetOfSystemSettings()
                End If
            Else
                Call ShowAndAllowSetOfSystemSettings()
            End If
            System.Environment.Exit(0)
        Catch ex As Exception
            systemLog.Fatal("System Error:", ex)
            System.Environment.Exit(1)

        End Try

    End Sub
    Shared Sub maintest()
        Try

            Dim myIE As New SHDocVw.InternetExplorer
            myIE.Navigate(".\table.html")

            Dim doc As mshtml.HTMLDocument = CType(myIE.Document, mshtml.HTMLDocument)
            Dim tbl As DispHTMLTable = CType(doc.getElementsByTagName("table").item(0), DispHTMLTable)

            Dim trows As mshtml.IHTMLElementCollection = tbl.rows
            Dim txns As List(Of Model.BankTransaction) = New List(Of Model.BankTransaction)

            'start row processing from 1 to length-2, so to skip header and footer row
            For i = 1 To trows.length - 2
                'Debug.WriteLine("")
                Dim row As mshtml.DispHTMLTableRow = CType(tbl.rows.item(i), DispHTMLTableRow)
                Dim txn As Model.BankTransaction = Model.BankTransactionFactory.Create
                txn.BankRefNo = CType(row.cells.item(0), DispHTMLTableCell).innerText
                txn.setPostDate(CType(row.cells.item(1), DispHTMLTableCell).innerText)
                txn.setValueDate(CType(row.cells.item(2), DispHTMLTableCell).innerText)
                txn.setDescription(CType(row.cells.item(3), DispHTMLTableCell).innerText)
                Dim lamnt As String = CType(row.cells.item(5), DispHTMLTableCell).innerText
                lamnt = lamnt.Replace(".", "") 'remove dots
                lamnt = lamnt.Replace(",", ".") ' replace comma with dot
                txn.setAmount(lamnt)

                txns.Add(txn)
            Next

            'Model.BankTransactionDataUtils.saveBankTransaction(txns.ToArray)

        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try

    End Sub
    Private Shared Sub ShowAndAllowSetOfSystemSettings()
        'show and allow set of system settings

        Dim f As New frmSettings
        If f.ShowDialog() = DialogResult.OK Then

            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "BOCPass", Encrypt.Encrypt(f.txtBOCPass.Text))
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "BOCUname", Encrypt.Encrypt(f.txtBOCUname.Text))
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "GUname", Encrypt.Encrypt(f.txtGUname.Text))
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "GPass", Encrypt.Encrypt(f.txtGPass.Text))

            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "Email1", f.txtEmail1.Text)
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "Email2", f.txtEmail2.Text)
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "SmtpServer", f.txtSmtpServer.Text)
            SaveSetting(REG_APP_NAME, REG_APP_MAIN, "SmtpPort", f.txtSmtpPort.Text)

        End If
    End Sub

End Class
